/*************************************
Copyright 2011 Chinua Iloabachie, Pradnya Khutafale, Siyi Liu, Junichi Suzuki, and The University of Massachusetts, Boston (http://code.google.com/p/tabletuml/)


Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 
*************************************/



// The UMLet source code is distributed under the terms of the GPL; see license.txt
package touchMe.Util;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;

import touchMe.baseClass.Constants;
import touchMe.baseClass.Diagram;
import touchMe.baseClass.Element;
import touchMe.baseClass.ElementType;

 

public class XMLContentHandler extends DefaultHandler {
	 
	private Element e;
	private Diagram umlDiagram;
	private String elementtext;

	private int x;
	private int y;
	private int w;
	private int h;
	private String entityname;
	private String code;
	private String panel_attributes;
	private String additional_attributes;

	 
	private ClassLoader fcl;

	// to be backward compatible - add list of old elements that were removed so that they are ignored when loading old files
	private List<String> ignoreElements;

	public XMLContentHandler() {
		/*this.handler = handler;
		_p = handler.getDrawPanel();*/
		
		umlDiagram= Diagram.getInstance();
		ignoreElements = new ArrayList<String>();
		ignoreElements.add("com.umlet.control.Group");
		//currentGroup = null;
		// use classloader of current thread (not systemclassloader - important for eclipse)
		this.fcl = Thread.currentThread().getContextClassLoader();
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) {
		elementtext = "";
		if (qName.equals("element")) {
			this.panel_attributes = "";
			this.additional_attributes = "";
			this.code = null;
			e= new Element();
			List<Element> entList=new ArrayList<Element>();
			 
		}
		if (qName.equals("group")) {
		/*	Group g = new Group();
			g.assignToDiagram(this.handler);
			if (currentGroup != null) currentGroup.addMember(g);
			currentGroup = g;*/
		}
	}

	@Override
	@SuppressWarnings("unchecked")
	public void endElement(String uri, String localName, String qName) {
		String elementname = qName; // [UB]: we are not name-space aware, so use the qualified name

		if (elementname.equals("help_text")) {
			// dont use the command because this causes an exception - dont know why - maybe a swing bug?!?
			//if (Umlet.getInstance().getGUI() != null) {
				umlDiagram.setHelpText(elementtext);
				//handler.setFontsize(HelpPanelChanged.getFontsize(elementtext));
			//}
		}
		else if (elementname.equals("zoom_level")) 
		 {
			if (umlDiagram != null) umlDiagram.setZoomLevel((Integer.parseInt(elementtext)));
		 }
		  /*else if (elementname.equals("group"))
		 *  {
			if (this.currentGroup != null) {
				this.currentGroup.adjustSize(false);
				_p.add(this.currentGroup);
				this.currentGroup = this.currentGroup.getGroup();
			}*/
		
		else if (elementname.equals("element")) {
			
			//if((e !=null)&&(!e.getType().equals(ElementType.RELATION)))
			if((e !=null))
				umlDiagram.getElements().add(e);
			if (!this.ignoreElements.contains(this.entityname)) { // load classes
				 
					if (this.code == null) {
						//java.lang.Class c = fcl.loadClass(this.entityname);
						//e.setType(type) = (Entity) c.newInstance();
					}
					//else e = CustomElementCompiler.getInstance().genEntity(this.code);
			
				//e.setState(this.panel_attributes);
				//e.setAdditionalAttributes(this.additional_attributes);
				//e.assignToDiagram(this.handler);

				//if (this.currentGroup != null) this.currentGroup.addMember(e);
				//_p.add(e);
			}
		}
		else if (elementname.equals("type")) {
			this.entityname = elementtext;			
			e.setType(elementtext);
			if(elementtext.equals(Constants.relationType)){
				
				// Pradnya commented Siyi's code
				//DiagramManager.checkPositiontoadd(x,y,w,h);
											
			}
				
		}
		else if (elementname.equals("x")) {
			Integer i = new Integer(elementtext);
			x = i.intValue();
			e.setX(x);
		}
		else if (elementname.equals("y")) {
			Integer i = new Integer(elementtext);
			y = i.intValue();
			e.setY(y);
		}
		else if (elementname.equals("w")) {
			Integer i = new Integer(elementtext);
			w = i.intValue();
			e.setW(w);
		}
		else if (elementname.equals("h")) {
			Integer i = new Integer(elementtext);
			h = i.intValue();
			e.setH(h);
		}
		//else if (elementname.equals("panel_attributes")) this.panel_attributes = elementtext;
		else if (elementname.equals("panel_attributes"))
			{
			   // relation type is based on panel attribute.So default
			  // type of relation is set to simple association. when panel
			  // attribute is set, reset the type attribute of relation
			  e.setPanelAttributes(new StringBuffer(elementtext.trim()));
			  String etype=e.getType().toString().trim();
			  if(etype.equals("TEMPORARY_ASSOCIATION"))
				  e.setType(Constants.relationType);
			}
		else if (elementname.equals("additional_attributes")) e.setAdditionalAttributes(new StringBuffer(elementtext)); 
		else if (elementname.equals("custom_code")) this.code = elementtext;
	
		
	}

	@Override
	public void characters(char[] ch, int start, int length) {
		elementtext += (new String(ch)).substring(start, start + length);
	}
	
	public Diagram getDiagram()
	{
		return umlDiagram;
	}

}
